home *** CD-ROM | disk | FTP | other *** search
/ MacFormat España 26 / macformat_26.iso / Shareware / Programación / C Reference Card / C Reference Card.rsrc / TEXT_411_11.txt < prev    next >
Text File  |  1997-01-29  |  3KB  |  117 lines

  1. TEMPLATES : template functions, template classes
  2. _________________________________________________________________________
  3.  
  4.  
  5. TEMPLATE FUNCTIONS
  6.  
  7. Anytime you find yourself writing several functions which perform the same function on different data types, you should considered writing a template function.  Although you could 'overload' a function to perform the same operations on each data type needed, template functions allow you to write the code once.  The compiler performs the work of creating the needed code for each data type used in the program.  Here's an example:
  8.  
  9.   // template.cp
  10.   #include <iostream.h>
  11.  
  12.   template <class T> T abs( T val )
  13.   {
  14.     if( val < 0 )
  15.       val = -val;
  16.     return val;
  17.   };
  18.  
  19.  
  20.   void main( void )
  21.   {
  22.     int   i1 = -4;
  23.     int   i2 = 237;
  24.     float f1 = -3.14;
  25.     float f2 = 5.83;
  26.  
  27.     cout << abs(i1) << endl;
  28.     cout << abs(i2) << endl;
  29.     cout << abs(f1) << endl;
  30.     cout << abs(f2) << endl;
  31.   }
  32.   // end template.cp
  33.  
  34. The keyword 'class' in the declaration 'template <class T>' has nothing to do with class structures, and should be read something like "a template of class T".
  35.  
  36.  
  37.  
  38. TEMPLATE CLASSES
  39.  
  40. You can also create template classes in much the same way as you create template functions.  Template classes can be used to define container classes to make it easy for you to create linked lists of all sorts of data without having to write a whole bunch of code.  The following is a simple example of using a template class that keeps a running total.
  41.  
  42.   // sum.cp
  43.   #include <iostream.h>
  44.  
  45.   template<class T> class Sum
  46.   {
  47.       T totalValue;
  48.       T lastValue;
  49.     public:
  50.       Sum();
  51.       ~Sum();
  52.       void add(T &);
  53.       T total(void);
  54.       T last(void);
  55.       void undo(void);
  56.   };
  57.  
  58.   template<class T> Sum<T>::Sum()
  59.   {
  60.     // constructor
  61.     totalValue = (T)0;
  62.     lastValue = (T)0;
  63.   }
  64.  
  65.   template<class T> Sum<T>::~Sum()
  66.   {
  67.     // destructor
  68.   }
  69.  
  70.   template<class T> void Sum<T>::add(T &val)
  71.   {
  72.     lastValue = val;
  73.     totalValue = totalValue + lastValue;
  74.   }
  75.  
  76.   template<class T> T Sum<T>::total(void)
  77.   {
  78.     return totalValue;
  79.   }
  80.  
  81.   template<class T> T Sum<T>::last(void)
  82.   {
  83.     return lastValue;
  84.   }
  85.  
  86.   template<class T> void Sum<T>::undo(void)
  87.   {
  88.     totalValue = totalValue - lastValue;
  89.     lastValue = -lastValue;
  90.   }
  91.  
  92.   void main( void )
  93.   {
  94.     Sum<int> RunningTotal;
  95.     
  96.     int i1 = 4;
  97.     int i2 = 8;
  98.     int i3 = 1;
  99.     int i4 = 7;
  100.     
  101.     RunningTotal.add(i1);
  102.     RunningTotal.add(i2);
  103.     RunningTotal.add(i3);
  104.     RunningTotal.add(i4);
  105.     
  106.     cout << "Total = " << RunningTotal.total() << endl;
  107.     cout << "Last Item = " << RunningTotal.last() << endl;
  108.  
  109.     cout << "Undo Last Operation..." << endl;
  110.     RunningTotal.undo();
  111.     cout << "Total = " << RunningTotal.total() << endl;
  112.  
  113.     cout << "Undo Last Operation (again)..." << endl;
  114.     RunningTotal.undo();
  115.     cout << "Total = " << RunningTotal.total() << endl;
  116.   }
  117.   // end sum.cp